﻿using DBUtils;
using LibUtils;
using ModelsUtils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models.JieShun;

namespace WebApi.Controllers
{
    public class CarStatusController : BaseController
    {
        #region 获取车辆状态  全部  已入场 已过皮重 已过总重 已出场
        /// <summary>
        /// 所有
        /// </summary>
        [AllowAnonymous]
        [HttpPost]
        public Result<List<CarStatus>> GetCarAllLists()
        {
            var list = GetRuChang();
            list.AddRange(GetPiZhong(1));
            list.AddRange(GetPiZhong(2));
            list.AddRange(GetChuChang());
            //var com = list.DistinctBy(p => p.FCarName).ToList();
            return new Result<List<CarStatus>>
            {
                code = (int)RespCodeEnum.Success,
                data = list,
                total = list.Count,
                msg = "成功"
            };
        }
        /// <summary>
        /// 已入场
        /// </summary>
        [AllowAnonymous]
        [HttpPost]
        public Result<List<CarStatus>> GetCarInLists()
        {
            var list = GetRuChang();
            return new Result<List<CarStatus>>
            {
                code = (int)RespCodeEnum.Success,
                data = list,
                total = list.Count,
                msg = "成功"
            };
        }

        /// <summary>
        /// 已过皮重
        /// </summary>
        [AllowAnonymous]
        [HttpPost]
        public Result<List<CarStatus>> GetCarTareLists()
        {
            var list = GetPiZhong(1);
            return new Result<List<CarStatus>>
            {
                code = (int)RespCodeEnum.Success,
                data = list,
                total = list.Count,
                msg = "成功"
            };
        }

        /// <summary>
        /// 已过总重
        /// </summary>
        [AllowAnonymous]
        [HttpPost]
        public Result<List<CarStatus>> GetCarTotalLists()
        {
            var list = GetPiZhong(2);
            return new Result<List<CarStatus>>
            {
                code = (int)RespCodeEnum.Success,
                data = list,
                total = list.Count,
                msg = "成功"
            };
        }
        /// <summary>
        /// 已出场
        /// </summary>
        [AllowAnonymous]
        [HttpPost]
        public Result<List<CarStatus>> GetCarOutLists()
        {
            var list = GetChuChang();
            return new Result<List<CarStatus>>
            {
                code = (int)RespCodeEnum.Success,
                data = list,
                total = list.Count,
                msg = "成功"
            };
        }

        private List<CarStatus> GetRuChang()
        {
            var list = new List<CarStatus>();
            var now = DateTime.Now.Date;
            var sql = $@" SELECT 
                                             a.inRecordId,
                                             a.inTime as FInTime,
                                             a.plateNumber as FCarName,
                                             b.Cargo_TiDanHao as FBillNo, 
                                        b.Cargo_YouPinMing as FWuliaoName, 
                                        b.Cargo_CheHao as FCarName, 
                                        b.isState as FStatus, 
                                       b. Cargo_ChuKuRiQi as FTotalTime , 
                                        b.Cargo_ZhuangHuoRiQi as FTareTime, 
                                        b.Cargo_ZongZhong as FTotalWeight, 
                                       b. Cargo_KongCheZhong as FTareWeight, 
                                        b.Cargo_JingZhong as FNetWeight ,
                                        b.Cargo_ShouHuoDanWei as FTenantName
                                     FROM DcsDB.dbo.carinM  as a
                                    left join 佛山惠福科创有限公司.dbo.Cargo as b on a.plateNumber=b.Cargo_CheHao
                                        WHERE inTime>='{now.AddDays(-7)}' ";
            var dt = DbHelperSQL.Query(sql).Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                list.Add(new CarStatus()
                {
                    FBillNo = item["FBillNo"].ToString(),
                    FCarName = item["FCarName"].ToString(),
                    FStatus = "已入场",
                    FWuliaoName = item["FWuliaoName"].ToString(),
                    FInTime = item["FInTime"].ToString(),
                    FNetWeight = item["FNetWeight"].ToString(),
                    FOutTime = item["FOutTime"].ToString(),
                    FTareWeight = item["FTareWeight"].ToString(),
                    FTotalWeight = item["FTotalWeight"].ToString(),
                    FTenantName = item["FTenantName"].ToString(),
                    FTareTime = item["FTareTime"].ToString(),
                    FTotalTime = item["FTotalTime"].ToString(),
                    //FStatusTime = s == 1 ? (Convert.ToDateTime(item["FCreatTime"].ToString())).ToString("yyyy-MM-dd HH:mm") : (Convert.ToDateTime(item["FOutTime"].ToString())).ToString("yyyy-MM-dd HH:mm")
                });
            }
            return list;
        }

        private List<CarStatus> GetPiZhong(int isState)
        {
            var list = new List<CarStatus>();
            var now = DateTime.Now.Date;
            var sql = $@" SELECT 
                                        Cargo_TiDanHao as FBillNo, 
                                        Cargo_YouPinMing as FWuliaoName, 
                                        Cargo_CheHao as FCarName, 
                                        isState as FStatus, 
                                        Cargo_ChuKuRiQi as FTotalTime , 
                                        Cargo_ZhuangHuoRiQi as FTareTime, 
                                        Cargo_ZongZhong as FTotalWeight, 
                                        Cargo_KongCheZhong as FTareWeight, 
                                        Cargo_JingZhong as FNetWeight ,
                                        Cargo_ShouHuoDanWei as FTenantName
                                   FROM 佛山惠福科创有限公司.dbo.Cargo  
                                   WHERE isState={isState} AND  Cargo_ZhuangHuoRiQi >='{now.AddDays(-7)}'";

            var dt = DbHelperSQL.Query(sql).Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                //var s = int.Parse(item["FStatus"].ToString());
                list.Add(new CarStatus()
                {
                    FBillNo = item["FBillNo"].ToString(),
                    FCarName = item["FCarName"].ToString(),
                    FStatus = isState==1?"已过皮重":"已过总重",
                    FWuliaoName = item["FWuliaoName"].ToString(),
                    FTareTime = item["FTareTime"].ToString(),
                    FTotalTime = item["FTotalTime"].ToString(),
                    //FInTime = item["FInTime"].ToString(),
                    FNetWeight = item["FNetWeight"].ToString(),
                    //FOutTime = item["FOutTime"].ToString(),
                    FTareWeight = item["FTareWeight"].ToString(),
                    FTotalWeight = item["FTotalWeight"].ToString(),
                    FTenantName = item["FTenantName"].ToString(),
                    //FStatusTime = s == 1 ? (Convert.ToDateTime(item["FCreatTime"].ToString())).ToString("yyyy-MM-dd HH:mm") : (Convert.ToDateTime(item["FOutTime"].ToString())).ToString("yyyy-MM-dd HH:mm")
                });
            }
            return list;
        }

        private List<CarStatus> GetChuChang()
        {
            var list = new List<CarStatus>();
            var now = DateTime.Now.Date;
            var sql = $@" SELECT 
                                             a.outRecordId,
                                             a.inRecordId,
                                             a.outTime as FOutTime,
                                             a.plateNumber as FCarName,
                                             b.Cargo_TiDanHao as FBillNo, 
                                        b.Cargo_YouPinMing as FWuliaoName, 
                                        b.Cargo_CheHao as FCarName, 
                                        b.isState as FStatus, 
                                       b. Cargo_ChuKuRiQi as FTotalTime , 
                                        b.Cargo_ZhuangHuoRiQi as FTareTime, 
                                        b.Cargo_ZongZhong as FTotalWeight, 
                                       b. Cargo_KongCheZhong as FTareWeight, 
                                        b.Cargo_JingZhong as FNetWeight ,
                                        b.Cargo_ShouHuoDanWei as FTenantName
                                     FROM DcsDB.dbo.caroutM  as a
                                    left join 佛山惠福科创有限公司.dbo.Cargo as b on a.plateNumber=b.Cargo_CheHao
                                        WHERE outTime>='{now.AddDays(-7)}' ";
            var dt = DbHelperSQL.Query(sql).Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                list.Add(new CarStatus()
                {
                    FBillNo = item["FBillNo"].ToString(),
                    FCarName = item["FCarName"].ToString(),
                    FStatus = "已入场",
                    FWuliaoName = item["FWuliaoName"].ToString(),
                    FInTime = item["FInTime"].ToString(),
                    FNetWeight = item["FNetWeight"].ToString(),
                    FOutTime = item["FOutTime"].ToString(),
                    FTareWeight = item["FTareWeight"].ToString(),
                    FTotalWeight = item["FTotalWeight"].ToString(),
                    FTenantName = item["FTenantName"].ToString(),
                    FTareTime = item["FTareTime"].ToString(),
                    FTotalTime = item["FTotalTime"].ToString(),
                    //FStatusTime = s == 1 ? (Convert.ToDateTime(item["FCreatTime"].ToString())).ToString("yyyy-MM-dd HH:mm") : (Convert.ToDateTime(item["FOutTime"].ToString())).ToString("yyyy-MM-dd HH:mm")
                });
            }
            return list;
        }

        #endregion
    }
}